<template>
  <!-- 审批列表 -->
  <div>
    <div style="display: flex; justify-content: flex-end">
      <el-select
        v-model="value"
        placeholder="请选择"
        @change="changeSelect"
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-input
        placeholder="请输入需求资源名称"
        v-model="name"
        class="input-with-select"
        style="width: 300px"
        clearable
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="searchBtn"
        ></el-button>
      </el-input>
    </div>

    <el-table
      :data="tableData"
      border
      style="width: 100%; margin: 10px 0"
      height="560"
    >
      <el-table-column
        fixed
        prop="dataName"
        label="需求资源名称"
        width="200"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="descri"
        label="预期成效"
        width="120"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="sqr"
        label="申请人"
        width="120"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="申请时间"
        align="center"
        width="200"
      >
        <template slot-scope="scope">
          {{ scope.row.createTime | formatDate }}
        </template>
      </el-table-column>
      <el-table-column
        prop="spzt"
        label="审批状态"
        width="120"
        align="center"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.spzt === 0">待数据局初审</span>
          <span v-else-if="scope.row.spzt === 1">初审通过，待数据源部门审核 </span>
          <span v-else-if="scope.row.spzt === 2">初审通过且已有数据编目</span>
          <span v-else-if="scope.row.spzt === 3">初审退回</span>
          <span v-else-if="scope.row.spzt === 4">待数据局复审</span>
          <span v-else-if="scope.row.spzt === 5">数据源部门退回</span>
          <span v-else-if="scope.row.spzt === 6">数据局复审退回</span>
          <span v-else-if="scope.row.spzt === 7">复审通过</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="spr"
        label="审核人"
        width="120"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="updateTime"
        label="审核时间"
        width="200"
        align="center"
      >
        <template slot-scope="scope" v-if="scope.row.updateTime && scope.row.updateTime !== null">
          {{ scope.row.updateTime | formatDate }}
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        align="center"
        label="操作"
        width="120">
        <template slot-scope="scope">
          <el-button @click="handleClickDetail(scope.row)" type="text" size="small">查看</el-button>
          <el-button @click="dbBtn(scope.row)" type="text" size="small" v-if="scope.row.spzt === 1">督办</el-button>
          <el-button type="text" size="small" @click="spBtn(scope.row)" v-if="scope.row.spzt === 0 || scope.row.spzt === 4 ">审批</el-button>
          <el-button type="text" size="small" @click="cxspBtn(scope.row)" v-if="scope.row.spzt === 5">重新审批</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="display: flex; justify-content: flex-end">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="prev, pager, next, sizes, jumper"
        :total="totalNum"
      >
      </el-pagination>
    </div>
    <!--        详情弹窗-->
    <el-dialog
      :visible.sync="dialogVisible"
      width="80%"
    >
      <el-descriptions title="申请详情">
        <el-descriptions-item label="需求资源名称">{{detailList.dataName}}</el-descriptions-item>
        <el-descriptions-item label="服务方式">
          {{detailList.type === 0
          ? '数据库'
          : detailList.type === 1
            ? 'API 接口'
            : detailList.type === 2
              ? '文件'
              : '未知类型'}}
        </el-descriptions-item>
        <el-descriptions-item label="数据项">{{ detailList.dataItem }}</el-descriptions-item>
        <el-descriptions-item label="资源覆盖范围">{{detailList.resourceRange}}</el-descriptions-item>
        <el-descriptions-item label="申请人名称">{{detailList.sqr}}</el-descriptions-item>
        <el-descriptions-item label="申请人手机号">{{detailList.phone}}</el-descriptions-item>
        <el-descriptions-item label="申请人邮箱">{{detailList.email}}</el-descriptions-item>
        <el-descriptions-item label="数据需求部门">{{detailList.appDept}}</el-descriptions-item>
        <el-descriptions-item label="需求描述">{{detailList.descri}}</el-descriptions-item>
        <el-descriptions-item label="使用起始时间">{{detailList.startTime | formatDate}}</el-descriptions-item>
        <el-descriptions-item label="使用结束时间">{{detailList.endTime | formatDate}}</el-descriptions-item>
        <el-descriptions-item label="应用场景">{{detailList.appScene}}</el-descriptions-item>
<!--        <el-descriptions-item label="出参">{{detailList.inputArg ? detailList.inputArg : '无'}}</el-descriptions-item>-->
<!--        <el-descriptions-item label="入参">{{detailList.outputArg ? detailList.outputArg : '无'}}</el-descriptions-item>-->
        <el-descriptions-item label="数据所属部门">{{detailList.dataDept}}</el-descriptions-item>
        <el-descriptions-item label="数据所属部门审批人">{{detailList.dataDeptSpr}}</el-descriptions-item>
        <el-descriptions-item label="数据局审批人">{{detailList.spr}}</el-descriptions-item>
        <el-descriptions-item label="数据局审批意见">{{detailList.spyj}}</el-descriptions-item>
        <el-descriptions-item label="数据所属部门审批意见">{{detailList.dataDeptSpyj}}</el-descriptions-item>
        <el-descriptions-item label="数据编目名称">{{detailList.sjbmId ? detailList.sjbmId :'暂无编目'}}</el-descriptions-item>

      </el-descriptions>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
<!--    审批弹窗-->
    <el-dialog title="审批" :visible.sync="dialogFormVisibleSp">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="审批结果" prop="spzt">
          <el-radio-group v-model="ruleForm.spzt">
            <el-radio :label="0">通过</el-radio>
            <el-radio :label="1">退回</el-radio>
            <el-radio :label="2" v-if="this.tgyysj  === 0">通过且已有数据</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审批意见" prop="auditReply">
          <el-input
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 4}"
            placeholder="请输入审批意见"
            v-model="ruleForm.auditReply">
          </el-input>
        </el-form-item>
        <div v-if="ruleForm.spzt === 0 && tgyysj != 4">
          <el-form-item label="数据源部门" prop="dataDept">
            <!-- LZYFIX 选择树点击强行使选择框组件失去焦点 -->
            <treeselect @click.native="$refs.select.blur()" v-model="selectedId" :multiple="false"   :options="optionsShu"  @input="handleInput" placeholder="请选择部门"/>
          </el-form-item>
          <el-form-item label="审批人" prop="dataDeptSpr">
            <el-select v-model="ruleForm.dataDeptSpr" placeholder="请选择审批人" ref="select">
              <el-option
                v-for="item in sprlist"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </div>
        <div v-if="tgyysj === 4 && ruleForm.spzt === 0 ">
          <el-form-item label="编目名称" prop="sjbmName">
            <el-input
              placeholder="请输入编目名称"
              v-model="ruleForm.sjbmName">
            </el-input>
          </el-form-item>
        </div>
        <div v-if="ruleForm.spzt === 2">
          <el-form-item label="编目名称" prop="sjbmName">
            <el-input
              placeholder="请输入编目名称"
              v-model="ruleForm.sjbmName">
            </el-input>
          </el-form-item>
        </div>

        <el-form-item>
          <el-button type="primary" @click="addSpBtn('ruleForm')">确定</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button @click="dialogFormVisibleSp = false">取 消</el-button>-->
<!--        <el-button type="primary" @click="dialogFormVisibleSp = false">确 定</el-button>-->
<!--      </div>-->
    </el-dialog>
  </div>
</template>
<script>
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import {
  getbysprList,
  xqlistDetail,
  getAuditdemand,
  getdept,
  getGeatadeptsprataDept,
  gecxtj,
  getAddsupervision
} from "@/utils/httpUtils";
import Treeselect from "@riophae/vue-treeselect";
export default {
  components: {Treeselect},
  data() {
    return {
      tgyysj:'',
      spId: '',
      sprlist:[],
      optionsShu: [],
      selectedId: null,
      radio: '',
      dialogFormVisibleSp:false,
      ruleForm: {
        spzt: '',
        auditReply:'',
        dataDeptSpr:'',
        sjbmName:''
      },
      rules: {
        spzt: [
          { required: true, message: '请选择审批状态', trigger: 'change' }
        ],
        auditReply: [
          { required: true, message: '请输入审批意见', trigger: 'blur' },
        ],
        dataDeptSpr:[
          {required: true, message: '请选择数据源部门审批人', trigger: 'change'}
        ],
        sjbmName:[
          {required: true, message: '请输入编目名称', trigger: 'change'}
        ]
      },
      formLabelWidth: '120px',
      dialogVisible: false,
      show: false,
      value: "",
      options: [
        {
          value: "",
          label: "全部",
        },
        {
          value: "0",
          label: "待数据局初审",
        },
        {
          value: "1",
          label: "初审通过，待数据源部门审核",
        },
        {
          value: "2",
          label: "初审通过且已有数据编目",
        },
        {
          value: "3",
          label: "初审退回",
        },
        {
          value: "4",
          label: "待数据局复审",
        },
        {
          value: "5",
          label: "数据源部门退回",
        },
        {
          value: "6",
          label: "数据局复审退回",
        },
        {
          value: "7",
          label: "复审通过",
        },
      ],
      name: "",
      tableData: [],
      activeName: "dataResource",
      currentPage: 1,
      pageNo: 1,
      pageSize: 10,
      status: "",
      totalNum: 0,
      detailsList: {
        resourceName: "",
        resourceType: "",
        applyInstitution: "",
        createUser: "",
        type: "",
        updateTime: "",
        description: "",
        createTime: "",
        updateUser: "",
        auditStatus: "",
      },
      updateCycleList: [],
      shareTypeList: [],
      ShareModeList: [],
      resourceType: 0,
      detailList:{}
    };
  },
  mounted() {
    this.initData()
    this.getdeptFn()
  },
  //   处理1703592547000 时间格式
  filters: {
    formatDate(timestamp) {
      const date = new Date(timestamp);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, "0");
      const day = String(date.getDate()).padStart(2, "0");
      const hours = String(date.getHours()).padStart(2, "0");
      const minutes = String(date.getMinutes()).padStart(2, "0");
      const seconds = String(date.getSeconds()).padStart(2, "0");

      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
  },
  methods: {
    dbBtn(row){
  this.$confirm('是否督办?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
    getAddsupervision({id:row.id}).then(res=>{
            console.log(res,"督办")
            if(res.code == 200){
              this.$message({
                type: 'success',
                message: res.msg
              });
              this.initData()
            }
          }).catch((err)=>{
            console.log(err,"err")
            this.$message({
              type: 'error',
              message: err.response.data.message
            });
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        })

    },
    addSpBtn(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const formattedRuleForm = {
            ...this.ruleForm,
            dataDept:this.selectedId,
            id:this.spId
          };
          getAuditdemand(formattedRuleForm).then(res=>{
            console.log(res,"新增123")
            this.$message({
              message: res,
              type: 'success'
            });

            // 关闭弹窗
            this.dialogFormVisibleSp = false
            // 清表单内容
            this.resetForm('ruleForm')
            //   重新渲染
            this.initData()
          }).catch(err=>{
            console.log(err,'err')
            this.$message({
              showClose: true,
              message: err.response.data.message,
              type: 'error'
            });
            // 清表单内容
            this.resetForm('ruleForm')
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },
    getdeptFn() {
      getdept().then(res => {
        console.log(res, "部门");
        // LZYFIX
        function clearChildren(arr) {
          console.log(arr,123333)
          arr.forEach(item => {
            if((item.children || []).length) {
              clearChildren(item.children);
            } else {
              item.children = void 0;
            }
          });
        }
        clearChildren([res[0]])
        // LZYFIX END
        this.optionsShu = [res[0]]; // 将根节点数据赋值给 options
        // this.processTree(this.options); // 处理数
      });
    },
    handleInput(value) {
      // 处理选中值
      this.selectedId = value;
      console.log(this.selectedId, "this.selectedId")
    //  调申请人接口
      getGeatadeptsprataDept(this.selectedId).then(res=>{
        console.log(res,"部门审批人")
        if(res.length>0){
          this.sprlist = res.map(item => ({
            value: item,
            label: item
          }));
        }else {
          this.sprlist = []
        }

      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    cxspBtn(row){
      this.dialogFormVisibleSp = true
      // console.log(row,'row')
      this.spId = row.id
      this.tgyysj = row.spzt
      console.log(row.spzt,"spzt")
      gecxtj({id:row.id}).then(res=>{
        console.log(res,"重新提交")
        // this.ruleFormCxtj.auditReply = res.dataDeptSpyj
        this.ruleForm.auditReply = res.dataDeptSpyj
      })
    },
    spBtn(row){
      this.dialogFormVisibleSp = true
      // console.log(row,'row')
      this.spId = row.id
      this.tgyysj = row.spzt
      console.log(row.spzt,"spzt")
    },
    handleClickDetail(row){
      console.log(row.id, "row")
      xqlistDetail({id:row.id}).then(res=>{
        console.log(res,'详情')
        this.dialogVisible = true
        this.detailList = res
      })
    },
    searchBtn() {
      this.initData();
    },

    changeSelect(val) {
      console.log(val, "val");
      this.status = val;
      this.initData();
    },
    // 初始化数据
    initData() {
      getbysprList({
        pageNum: this.pageNo,
        pageSize: this.pageSize,
        dataName: this.name,
        spzt:this.value,
      }).then((res) => {
        console.log(res, "我的申请，数据资源123123");
        this.tableData = res.content;
        this.totalNum = res.totalElements;
        this.name = "";
      });
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.initData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNo = val;
      this.initData();
    },
  },
};
</script>
<style  scoped>
.listToggle {
  padding: 13px 20px;
}

.listToggle.backBtn {
  border-bottom: 4px solid #198ef8;
}
</style>


